<template>
    <div>
        <el-card class="box-card">
            <el-tabs v-model="activeName">
                <el-tab-pane label="轮播图" name="first">
                    <el-row style="padding-top: 15px;">
                        <el-row>
                            <el-col :span="8">
                                <span>校区：</span>
                                <el-select v-model="school" placeholder="请选择" clearable size="mini"
                                    @change="choiceSchool()">
                                    <el-option v-for="item in schools" :key="item.id" :label="item.campusName"
                                        :value="item.id">
                                    </el-option>
                                </el-select>
                                <el-button type="primary" el-icon-plus size="mini" @click="getCurrentPageData()">
                                    查询</el-button>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="4" :offset="20">
                                <el-button type="primary" el-icon-plus size="mini" icon="el-icon-plus"
                                    @click="dialogFormVisible = true"> 添加</el-button>
                                <el-popconfirm confirm-button-text='好的' cancel-button-text='取消' icon="el-icon-info"
                                    icon-color="red" @confirm="handleDelete()" title="确定删除吗？">
                                    <el-button type="danger" size="mini" slot="reference"
                                        :disabled="multiple">删除</el-button>
                                </el-popconfirm>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-table :data="tableData" stripe style="margin-top: 20px;" border
                                @selection-change="handleSelectionChange">
                                <el-table-column type="selection" width="55">
                                </el-table-column>
                                <el-table-column  label="轮播图">
                                    <template slot-scope="scope">
                                        <img :src=userUrl+scope.row.img style="width: 100px;">
                                    </template>
                                </el-table-column>
                                <el-table-column prop="headline" label="标题">
                                </el-table-column>
                                <el-table-column prop="link" label="链接">
                                </el-table-column>
                                <el-table-column label="编辑" width="180">
                                    <template slot-scope="scope">
                                        <el-link :underline="false" type="primary" style="font-size: 12px"
                                            @click="update(scope)">编辑</el-link>
                                        <el-link :underline="false" type="primary"
                                            style="font-size: 12px" @click="del(scope.row.id)">删除</el-link>
                                    </template>
                                </el-table-column>
                            </el-table>
                        </el-row>
                        <el-row>
                            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                                :current-page="currentPage" :page-sizes="[7]" :page-size="size"
                                layout="sizes, prev, pager, next, jumper,total" :total="total">
                            </el-pagination>
                        </el-row>
                    </el-row>
                </el-tab-pane>
                <el-tab-pane label="关于学校" name="second">配置管理</el-tab-pane>
                <el-tab-pane label="学校风采" name="third">角色管理</el-tab-pane>
            </el-tabs>
        </el-card>
        <el-dialog :visible.sync="dialogFormVisible">
            <span style="font-weight: bold; font-size: large; margin-right: 80%;">添加动态分类</span>
            <el-divider></el-divider>
            <el-form :model="form" size="mini">
                <el-form-item label="所属校区" style="margin-left: 30%;">
                    <el-select v-model="form.school1" placeholder="请选择" style="margin-right: 42%;" clearable
                        @change="choiceSchool()">
                        <el-option v-for="item in schools1" :key="item.id" :label="item.campusName" :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="标题" style="margin-left: 34%;">
                    <el-input v-model="form.headline" style="width: 200px ;margin-right: 44%;"></el-input>
                </el-form-item>
                <el-form-item label="链接" style="margin-left: 34%;">
                    <el-input v-model="form.link" style="width: 200px ;margin-right: 44%;"></el-input>
                </el-form-item>
                <el-form-item label="轮播图" style="margin-left: 32%;">
                    <el-upload class="upload-demo" action="https://up-z2.qiniup.com" :on-preview="handlePreview"
                        :on-remove="handleRemove" :on-success="uploadSuccess" :on-error="fail" :file-list="fileList"
                        :data="uploadData" list-type="img" style="margin-right: 67%;">
                        <el-button size="small" type="primary">上传图片</el-button>
                    </el-upload>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer" style="margin-bottom: 40px;">
                <el-button type="primary" @click="insert()" size="mini">确 定</el-button>
                <el-button @click="dialogFormVisible = false" size="mini">取 消</el-button>
            </div>
        </el-dialog>
        <el-dialog :visible.sync="dialogFormVisibleupdate">
            <span style="font-weight: bold; font-size: large; margin-right: 80%;">添加动态分类</span>
            <el-divider></el-divider>
            <el-form :model="form" size="mini">
                <el-form-item label="标题" style="margin-left: 34%;">
                    <el-input v-model="updateForm.headline" style="width: 200px ;margin-right: 44%;"></el-input>
                </el-form-item>
                <el-form-item label="链接" style="margin-left: 34%;">
                    <el-input v-model="updateForm.link" style="width: 200px ;margin-right: 44%;"></el-input>
                </el-form-item>
                <el-form-item label="轮播图" style="margin-left: 32%;">
                    <el-upload class="upload-demo" action="https://up-z2.qiniup.com" :on-preview="handlePreview"
                        :on-remove="handleRemove" :on-success="uploadSuccess" :on-error="fail" :file-list="fileList"
                        :data="uploadData" list-type="img" style="margin-right: 67%;">
                        <el-button size="small" type="primary">上传图片</el-button>
                    </el-upload>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer" style="margin-bottom: 40px;">
                <el-button type="primary" @click="commit()" size="mini">确 定</el-button>
                <el-button @click="dialogFormVisibleupdate = false" size="mini">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
export default {
    data() {
        return {
            activeName: 'first',
            school: '',
            schools: [],
            schools1: [],
            tableData: [],
            currentPage: 1,     // 当前页页码
            size: 7,            // 当前页大小
            total: 0,
            //删除
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            //添加轮播图
            dialogFormVisible: false,
            form: {
                img: '',
                headline: '',
                link:''
            },
            formLabelWidth: '80px',
            //更新轮播图
            updateForm: {
                img: '',
                headline: '',
                link:''
            },
            dialogFormVisibleUpdate: false,
            //显示图片
            uploadData: {
                token: ""
            },
            fileList: [],
            userUrl: "http://s0z90zmfa.hn-bkt.clouddn.com/",
            userImage: '',
            aaa:'',
            img:'',

            dialogFormVisibleupdate:false
        };
    },
    methods: {
        fail(e) {
            console.log(e)
        },
        //上传成功以后，把返回的文件hash交给user中，一起传到后端。
        uploadSuccess(res) {
            this.img = res.hash;
        },
        handleRemove() {

        },
        handlePreview() {

        },
        handleRemove(file, fileList) {
            console.log(file, fileList);
        },
        handlePictureCardPreview(file) {
            this.dialogImageUrl = file.url;
            this.dialogVisible = true;
        },
        choiceSchool: function () {
            console.log(this.school)
        },
        //分页
        handleSizeChange(val) { // 页面大小修改时触发
            console.log(`1每页 ${val} 条`);
        },
        handleCurrentChange(val) { // 页码修改时触发
            console.log(`1当前页: ${val}`);
            // 给当前页属性赋值
            this.currentPage = val
            // 发请求获取数据
            this.getCurrentPageData()
        },
        getCurrentPageData: function () {
            // 页面加载时需要将当前页、页大小作为参数传给后台  key=value 传值
            //  /money/tenement?page=1&size=10
            let data = {
                img: this.tableData.img,
                headline: this.tableData.headline,
                link: this.tableData.link,
                page: this.currentPage,
                schoolId: this.school,
                size: this.size
            }
            this.$axios.post("/aboutSchool/findSlideShowByPage", data).then(res => {
                console.log(res.data)
                this.tableData = res.data.data.data
                // 总条数
                this.total = res.data.data.total
                console.log(this.tableData);
            })
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            console.log(selection);
            this.ids = selection.map(item => item.id);// 需要根据数据情况调整id名称
            console.log(this.ids);
            this.single = selection.length != 1;
            this.multiple = !selection.length;
        },
        handleDelete() {
            //传数组进行批量删除
            this.$axios.post("/aboutSchool/delSlideShow", this.ids).then(result => {
                if (result.data.data == true) {
                    //this.loadOperLogByPage(this.current);
                    this.$message.success("删除成功")
                    this.getCurrentPageData()
                }
            })
        },
        insert: function () {
            let data = {
                img: this.img,
                headline:this.form.headline,
                link:this.form.link,
                schoolId: this.form.school1,
            }
            this.$axios.post("/aboutSchool/insertSlideShow", data).then(res => {
                this.$message.success("添加成功")
                this.dialogFormVisible = false
                this.getCurrentPageData()
                this.form=''
            })
        },
        update: function (scope) {
            console.log(scope)
            this.updateForm = JSON.parse(JSON.stringify(scope.row))
            console.log(this.updateForm)
            this.currentIndex = scope.$index
            this.dialogFormVisibleupdate = true
        },
        commit: function () {
            // 先提交数据修改后台数据库，如果修改成功，再修改前端的
            this.$axios.post("/aboutSchool/updateSlideShow", this.updateForm).then(res => {
                console.log(res.data)
                if (res.data.code == 200) {
                    this.$message.success("修改成功")
                    // 修改前端，用副本替换掉原来的数据就行
                    this.getCurrentPageData()
                } else {
                    this.$message.error("修改失败，请稍后再试")
                }
            })
            // 关闭对话框
            this.dialogFormVisibleupdate = false
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            console.log(selection);
            this.ids = selection.map(item => item.id);// 需要根据数据情况调整id名称
            console.log(this.ids);
            this.single = selection.length != 1;
            this.multiple = !selection.length;
        },
        handleDelete() {
            //传数组进行批量删除
            this.$axios.post("/aboutSchool/delSlideShow", this.ids).then(result => {
                if (result.data.data == true) {
                    //this.loadOperLogByPage(this.current);
                    this.$message.success("删除成功")
                    this.getCurrentPageData()
                }
            })
        },
        del:function(id){
            this.$axios.delete("/aboutSchool/del/"+id).then(res =>{
                console.log();
                if (res.data.data == true) {
                    //this.loadOperLogByPage(this.current);
                    this.$message.success("删除成功")
                    this.getCurrentPageData()
                }
            })
        }
    },
    mounted() {
        this.getCurrentPageData(), 
        this.$axios.get("/dynamic/choiceSchool").then(res => {
            this.schools = res.data.data
            this.schools1 = res.data.data
        })
        this.$axios.get("/dynamic/getUpdateToken")
            .then(res => {
                this.uploadData.token = res.data.data;
        })
    }
};
</script>

<style scoped></style>